<?php

namespace App\ApiBackend\Site;

use App\Http\Controllers\ApiBaseController;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;

/**
 *  编辑
 */
class Update extends ApiBaseController
{
  /**
   * 参数检查
   */
  protected function check(): void
  {
    // 定义规则
    $ruleArr = [
      'name' => 'required',
    ];
    $messageArr = [
      'required' => ':attribute必须填写',
    ];
    //验证
    $validator = Validator::make(request()->all(), $ruleArr, $messageArr, [
    ]);
    //验证失败
    if ($validator->stopOnFirstFailure()->fails()) {

      $this->result["code"] = 1;
      $this->result["message"] = $validator->errors()->first();
    }
  }

  /**
   * 业务主体
   */
  protected function service(): void
  {
    $id = 1;
    //数据
    $insertData = [
      "name" => request('name'),
      "email" => request('email'),
      "tel" => request('tel'),
      "seoTitle" => request('seoTitle'),
      "seoKeyword" => request('seoKeyword'),
      "seoDescription" => request('seoDescription'),
    ];
    try {
      DB::table('site')->where('id', $id)->update($insertData);
      Cache::store('redis')->forget('siteInfo');
    } catch (\Exception $exception) {
      $this->result["code"] = 1;
      $this->result["message"] = $exception->getMessage();
    }
    return;
  }
}

